import numpy as np
import matplotlib.pyplot as plt

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
Y = np.array([0, 1, 1, 0])
print(X.shape)
print(Y.shape)


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:,0], Y, X[:,1], c='r', marker='o')


no_of_inputs = X.shape[1]
weights = np.random.rand(no_of_inputs + 1)
print(weights.shape)


for i in range(len(X)):
  inputs = X[i]
  print(inputs)
  summation = np.dot(inputs, weights[1:]) + weights[0]
  print(summation)


learning_rate = .1
epochs = 100
history = []
for _ in range(epochs):
  for inputs, label in zip(X, Y):
    prediction = summation = np.dot(inputs, weights[1:]) + weights[0]
    loss = label - prediction
    history.append(loss*loss)
    print(f"loss = {loss*loss}")
    weights[1:] += learning_rate * loss * inputs
    weights[0] += learning_rate * loss


for i in range(len(X)):
  inputs = X[i]
  print(inputs)
  summation = np.dot(inputs, weights[1:]) + weights[0]
  print(summation)
# plt.plot(history)
#原画图存在TypeError问题
plt.figure(figsize=(10, 5))
plt.plot(history, label='Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss over Epochs')
plt.legend()
plt.show()
